package com.dji.compose_learning

import android.os.Bundle
import android.util.Log
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.dji.compose_learning.net.GithubUser
import com.dji.compose_learning.net.getGithubService
import com.dji.compose_learning.viewmodel.CoroutineSampleViewModel
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.channels.actor
import kotlinx.coroutines.channels.produce
import kotlin.coroutines.*

class CoroutineSampleActivity : ComponentActivity() {
    val viewModel by viewModels<CoroutineSampleViewModel>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        setContent {
//            CoroutinePageView()
//        }
        setContentView(R.layout.activity_coroutine)
        //全局协程
        GlobalScope.launch(Dispatchers.Main) {
            val userlist = withContext(Dispatchers.IO) {
                Log.i("buobao1", Thread.currentThread().name)
                delay(3000)
                getGithubService().getUsers(0)
            }
            Log.i("buobao2", Thread.currentThread().name)
            findViewById<TextView>(R.id.user_count).text = "共计获取用户数量:${userlist.size}"
        }

        //runBlocking会阻塞当前线程
        /*
        val userList = runBlocking(Dispatchers.IO) {
            Log.i("buobao3", Thread.currentThread().name)
//            delay(9000)
            getGithubService().getUsers(0)
        }
        findViewById<TextView>(R.id.user_name).text = userList[0]?.login
        */

        val continuation = suspend {
            getGithubService().getUsers(0)
        }.createCoroutine(object : Continuation<List<GithubUser?>> {
            override val context: CoroutineContext
                get() = EmptyCoroutineContext

            override fun resumeWith(result: Result<List<GithubUser?>>) {
                Log.i("buobao6", "success")
            }
        })
        continuation.resume(Unit)

        suspend {
            Log.i("buobao4", Thread.currentThread().name)
            getGithubService().getUsers(0)
        }.startCoroutine(object : Continuation<List<GithubUser?>> {
            override val context: CoroutineContext
                get() = EmptyCoroutineContext

            override fun resumeWith(result: Result<List<GithubUser?>>) {
                Log.i("buobao5", Thread.currentThread().name)
            }
        })

        //什么是结构化并发
        //协程作用域的用途
        CoroutineScope(EmptyCoroutineContext + SupervisorJob()).launch { }

        //ContextScope(SupervisorJob() + Dispatchers.Main) 等于
//        MainScope().launch {
//
//        }

        //produce作为一个便携式封装生产者协程和channel
        val receivechannel: ReceiveChannel<Int> = lifecycleScope.produce {
            repeat(10) {
                delay(500)
                send(it)
            }
            cancel()
        }

        lifecycleScope.launch {
            for (item in receivechannel) {
                println("receive $item")
            }
        }

        //actor可以构建一个便携的消费者协程
        val sendChannel: SendChannel<Int> = lifecycleScope.actor {
            for (item in channel) {
                println("receive $item")
            }
        }

        lifecycleScope.launch {
            repeat(10) {
                sendChannel.send(it * 10)
            }
            sendChannel.close()
        }
    }
}

suspend fun add() = coroutineScope {

}

suspend fun add1() = supervisorScope {

}























